package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.entity.business.common.ShipFormation;
import com.xcf.service.business.common.shipformation.ShipFormationService;
import com.xcf.util.PageData;

@Controller
@RequestMapping(value = "/ship")
public class ShipinFormationController extends BaseController {

	@Resource
	private ShipFormationService shipformationservice;
	String menuUrl = "ship/information"; // 菜单地址(权限用)

	@RequestMapping(value = "/information")
	// 条件查询及列表查询
	public ModelAndView ship(Page page) {
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();	
		try {
			pd = this.getPageData();
			String VEHICLE_TYPE = pd.getString("VEHICLE_TYPE");							
			if(null != VEHICLE_TYPE && !"".equals(VEHICLE_TYPE)){
				pd.put("VEHICLE_TYPE", VEHICLE_TYPE.trim());
			}
			String SHIP_NAME_ID = pd.getString("SHIP_NAME_ID");							
			if(null != SHIP_NAME_ID && !"".equals(SHIP_NAME_ID)){
				pd.put("SHIP_NAME_ID", SHIP_NAME_ID.trim());
			}
			String VOYAGE_NUMBER = pd.getString("VOYAGE_NUMBER");							
			if(null != VOYAGE_NUMBER && !"".equals(VOYAGE_NUMBER)){
				pd.put("VOYAGE_NUMBER", VOYAGE_NUMBER.trim());
			}
			String SCHEDULED_SAILING_TIME = pd.getString("SCHEDULED_SAILING_TIME");	
			if(null != SCHEDULED_SAILING_TIME && !"".equals(SCHEDULED_SAILING_TIME)){
				pd.put("SCHEDULED_SAILING_TIME", SCHEDULED_SAILING_TIME.trim());
			}
			String PLANNED_ARRIVAL_TIME = pd.getString("PLANNED_ARRIVAL_TIME");	
			if(null != PLANNED_ARRIVAL_TIME && !"".equals(PLANNED_ARRIVAL_TIME)){
				pd.put("PLANNED_ARRIVAL_TIME", PLANNED_ARRIVAL_TIME.trim());
			}
			page.setPd(pd);
			
			List<ShipFormation>list=shipformationservice.seach(page);
			List<PageData>list1=shipformationservice.seachShip();
			mv.setViewName("/business/common/shipinformation/shipinformation");
			mv.addObject("list", list);
			mv.addObject("list1", list1);//航线
			mv.addObject("pd", pd);
		} catch (Exception e) {

			e.printStackTrace();
		}

		return mv;

	}
	
	//去新增页面
		@RequestMapping(value="/shipadd")
		public ModelAndView shipadd(){
			ModelAndView mv = new ModelAndView();
	
			try {
				List<PageData>list=shipformationservice.seachShip();
				List<PageData>list1=shipformationservice.query();
				mv.addObject("list", list);//船名
				mv.addObject("list1", list1);//航线
				 mv.setViewName("/business/common/shipinformation/shipinformation_add");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			  return mv;
			}
		
		//点保存一条及修改
		@SuppressWarnings("unused")
		@RequestMapping(value="saveU")
		@ResponseBody
		public String saveU() throws Exception{
			PageData pd = new PageData();
			    pd = this.getPageData();
			    String SCHEDULED_SAILING_TIME =(String) pd.get("SCHEDULED_SAILING_TIME");
				if(!SCHEDULED_SAILING_TIME.isEmpty()&&pd.getString("ID")==null||pd.getString("ID")==""){
					PageData a=shipformationservice.search(pd);
					if (a==null){
						String arr[] = pd.getString("CARRIER").split("@");
						if(  arr.length > 1|| arr == null){
						pd.put("CYR_ID", arr[0]);
						pd.put("CYR", arr[1]);
						shipformationservice.addship(pd);
						}else{
						shipformationservice.addship(pd);}
					}else{
						String BILL_CODE=a.getString("BILL_CODE");//托单编号
						String LADING_BILL_CODE=a.getString("LADING_BILL_CODE");//提单编号
						String BUSINESS_TYPE_ID= a.getString("BUSINESS_TYPE_ID");//业务类型ID
						String INCONE_REMARK=a.getString("INCONE_REMARK");//收入备注
						String CONTAINER_OWNER=a.getString("String");//箱主
						String BILL_INPUTER=a.getString("BILL_INPUTER");//操作人
						if(BUSINESS_TYPE_ID.equals("3")||BUSINESS_TYPE_ID.equals("4")){//内贸
						List<PageData>list2=shipformationservice.DomesticTrade();
						for (PageData dom : list2) {
							String BOX_TYPE=dom.getString("BOX_TYPE");//箱型
							String FEE_NAME=dom.getString("FEE_NAME");//费用名称
							//dom.getString("UNIT_PRICE");//单价
							dom.getString("RIVER_BOAT_COMPANY");//江船公司
							String SETTLE_UNIT =dom.getString("SETTLE_UNIT");//结算单位
						    PageData phq=new PageData();
						    phq.put("BILL_CODE", BILL_CODE);//托单编号
						    phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
						    phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
						    phq.put("BOX_TYPE", BOX_TYPE);//箱型
						    phq.put("FEE_NAME", FEE_NAME);//费用名称
						    phq.put("UNIT_PRICE", dom.get("UNIT_PRICE"));//单价
						    phq.put("SETTLE_UNIT", SETTLE_UNIT);//结算单位
						    shipformationservice.adddome(phq);//新增收款
						    shipformationservice.adddomestic(phq);//新增付款
						}
						
						}else if(BUSINESS_TYPE_ID.equals("1")||BUSINESS_TYPE_ID.equals("2")){//外贸
						List<PageData>	list=shipformationservice.searchForeign();
						for (PageData foreign : list) {
							String BOAT_COMPANY=foreign.getString("BOAT_COMPANY");//船公司名称
							String SETTLE_UNIT=foreign.getString("SETTLE_UNIT");//结算单位
							String FEE_NAME=foreign.getString("FEE_NAME");//费用名称
						      //foreign.get("FEE_PRICE");//费用价格
						    String CURRENCY_TYPE=foreign.getString("CURRENCY_TYPE");//币种类型
						    String BOX_TYPE=foreign.getString("BOX_TYPE");//箱型
						    String REMARK=foreign.getString("REMARK");//备注
			
							PageData phq=new PageData();
							phq.put("BILL_CODE", BILL_CODE); //托单编号
							phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
							phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
							phq.put("SETTLE_UNIT", SETTLE_UNIT); //结算单位
							phq.put("BOX_TYPE", BOX_TYPE); //箱型
			                phq.put("REMARK", REMARK);//备注
			                phq.put("CURRENCY_TYPE", CURRENCY_TYPE);//币种类型
			                phq.put("FEE_NAME", FEE_NAME);//费用名称
			               // phq.put("FEE_PRICE",  foreign.get("FEE_PRICE"));
							//PageData A=shipformationservice.seacByCode(CONSIGNMENT_BILL_NUMBER);
//							if(A==null){
								shipformationservice.addsForeign(phq);//新增收款账单
								shipformationservice.addscope(phq); //新增付款账单
//							}
						}
					
						List<PageData>	list1=shipformationservice.searchTrade();
						for (PageData trade : list1) {
							String SETTLE_UNIT=trade.getString("SETTLE_UNIT");//结算单位
							String BOX_MASTER=trade.getString("BOX_MASTER");//箱主
							String CURRENCY_TYPE=trade.getString("CURRENCY_TYPE");//货币类型
							//trade.getString("EXCHANGE_RATE");//汇率
							//String ab=trade.getString("UNIT_PRICE");//单价
//							Float   UNIT_PRICE=Float.parseFloat(trade.get("UNIT_PRICE"));
							String FRR_NAME=trade.getString("FRR_NAME");//费用名称
							String REMARK=trade.getString("REMARK");//备注
							PageData phq= new PageData();
							phq.put("BILL_CODE", BILL_CODE); //托单编号
							phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
							phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
							phq.put("SETTLE_UNIT", SETTLE_UNIT); //结算单位
							//phq.put("BOX_MASTER", BOX_MASTER); //箱主
			                phq.put("REMARK", REMARK);//备注
			                phq.put("CURRENCY_TYPE", CURRENCY_TYPE);//币种类型
			                phq.put("FEE_NAME", FRR_NAME);//费用名称
			                phq.put("UNIT_PRICE",trade.get("UNIT_PRICE") );//单价
			               // phq.put("EXCHANGE_RATE", trade.getString("EXCHANGE_RATE"));//汇率
			                shipformationservice.addstrade(phq);//新增收款账单
							shipformationservice.addtrade(phq); //新增付款账单
						}
						
						}
						
						String arr[] = pd.getString("CARRIER").split("@");
						if(  arr.length > 1|| arr == null){
							pd.put("CYR_ID", arr[0]);
							pd.put("CYR", arr[1]);
							shipformationservice.addship(pd);
							}else{
						
						shipformationservice.addship(pd);}
					}
					
					
					
				}else{
					 if(!SCHEDULED_SAILING_TIME.isEmpty()&&pd.getString("ID")!=null||pd.getString("ID")==""){
						PageData a=shipformationservice.search(pd);
						if(a==null){
							shipformationservice.updateShip(pd);
						}else{
							String BILL_CODE=a.getString("BILL_CODE");//托单编号
							String LADING_BILL_CODE=a.getString("LADING_BILL_CODE");//提单编号
							String BUSINESS_TYPE_ID= a.getString("BUSINESS_TYPE_ID");//业务类型ID
							String BILL_INPUTER=a.getString("BILL_INPUTER");//操作人
							if(BUSINESS_TYPE_ID.equals("1")||BUSINESS_TYPE_ID.equals("2")){ //判断外贸
								List<PageData>	list=shipformationservice.searchForeign();
								for (PageData foreign : list) {
									String BOAT_COMPANY=foreign.getString("BOAT_COMPANY");//船公司名称
									String SETTLE_UNIT=foreign.getString("SETTLE_UNIT");//结算单位
									String FEE_NAME=foreign.getString("FEE_NAME");//费用名称
								      //foreign.get("FEE_PRICE");//费用价格
								    String CURRENCY_TYPE=foreign.getString("CURRENCY_TYPE");//币种类型
								    String BOX_TYPE=foreign.getString("BOX_TYPE");//箱型
								    String REMARK=foreign.getString("REMARK");//备注
					
									PageData phq=new PageData();
									phq.put("BILL_CODE", BILL_CODE); //托单编号
									phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
									phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
									phq.put("SETTLE_UNIT", SETTLE_UNIT); //结算单位
									phq.put("BOX_TYPE", BOX_TYPE); //箱型
					                phq.put("REMARK", REMARK);//备注
					                phq.put("CURRENCY_TYPE", CURRENCY_TYPE);//币种类型
					                phq.put("FEE_NAME", FEE_NAME);//费用名称
					               // phq.put("FEE_PRICE",  foreign.get("FEE_PRICE"));
									List<PageData> A=shipformationservice.seacByCode(phq);
									List<PageData> B=shipformationservice.seacByCo(phq);
									if(A.isEmpty()&&B.isEmpty()){
										shipformationservice.addsForeign(phq);//新增收款账单
										shipformationservice.addscope(phq); //新增付款账单
									}else{
										shipformationservice.updateForeign(phq);//修改收款账单
										shipformationservice.updatescope(phq);//修改付款账单
									}
								}
								List<PageData>	list1=shipformationservice.searchTrade();
								for (PageData trade : list1) {
									String SETTLE_UNIT=trade.getString("SETTLE_UNIT");//结算单位
									String BOX_MASTER=trade.getString("BOX_MASTER");//箱主
									String CURRENCY_TYPE=trade.getString("CURRENCY_TYPE");//货币类型
									//trade.getString("EXCHANGE_RATE");//汇率
									//String ab=trade.getString("UNIT_PRICE");//单价
//									Float   UNIT_PRICE=Float.parseFloat(trade.get("UNIT_PRICE"));
									String FRR_NAME=trade.getString("FRR_NAME");//费用名称
									String REMARK=trade.getString("REMARK");//备注
									PageData phq= new PageData();
									phq.put("BILL_CODE", BILL_CODE); //托单编号
									phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
									phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
									phq.put("SETTLE_UNIT", SETTLE_UNIT); //结算单位
									//phq.put("BOX_MASTER", BOX_MASTER); //箱主
					                phq.put("REMARK", REMARK);//备注
					                phq.put("CURRENCY_TYPE", CURRENCY_TYPE);//币种类型
					                phq.put("FEE_NAME", FRR_NAME);//费用名称
					                phq.put("UNIT_PRICE",trade.get("UNIT_PRICE") );//单价
					               // phq.put("EXCHANGE_RATE", trade.getString("EXCHANGE_RATE"));//汇率
					                List<PageData> A=shipformationservice.seacByCode(phq);
									List<PageData> B=shipformationservice.seacByCo(phq);
					                if(A.isEmpty()&&B.isEmpty()){
					                	shipformationservice.addstrade(phq);//新增收款账单
										shipformationservice.addtrade(phq); //新增付款账单
					                }else{
					                	shipformationservice.updatestrade(phq);//修改收款账单
										shipformationservice.updatetrade(phq);//修改付款账单
					                }
					                
								}
								
							}if(BUSINESS_TYPE_ID.equals("3")||BUSINESS_TYPE_ID.equals("4")){//判断内贸
								List<PageData>list2=shipformationservice.DomesticTrade();
								for (PageData dom : list2) {
									String BOX_TYPE=dom.getString("BOX_TYPE");//箱型
									String FEE_NAME=dom.getString("FEE_NAME");//费用名称
									//dom.getString("UNIT_PRICE");//单价
									dom.getString("RIVER_BOAT_COMPANY");//江船公司
									String SETTLE_UNIT =dom.getString("SETTLE_UNIT");//结算单位
								    PageData phq=new PageData();
								    phq.put("BILL_CODE", BILL_CODE);//托单编号
								    phq.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
								    phq.put("BILL_INPUTER", BILL_INPUTER);//操作人
								    phq.put("BOX_TYPE", BOX_TYPE);//箱型
								    phq.put("FEE_NAME", FEE_NAME);//费用名称
								    phq.put("UNIT_PRICE", dom.get("UNIT_PRICE"));//单价
								    phq.put("SETTLE_UNIT", SETTLE_UNIT);//结算单位
								    List<PageData> A=shipformationservice.seacByCode(phq);
									List<PageData> B=shipformationservice.seacByCo(phq);
									  if(A.isEmpty()&&B.isEmpty()){
										  shipformationservice.adddome(phq);//新增收款
										    shipformationservice.adddomestic(phq);//新增付款 
									  }else{
										    shipformationservice.updatedome(phq);//修改收款账单
											shipformationservice.updatedomestic(phq);//修改付款账单
									  }
								   
								}
								
							}
						}
						
					}
					if(pd.getString("ID")==null || pd.getString("ID")==""){
						String arr[] = pd.getString("CARRIER").split("@");
						if(  arr.length > 1|| arr == null){
							pd.put("CYR_ID", arr[0]);
							pd.put("CYR", arr[1]);
							shipformationservice.addship(pd);
						}else{
							shipformationservice.addship(pd);}
							
					}
							
					else{
						String arr[] = pd.getString("CARRIER").split("@");
						pd.put("CYR_ID", arr[0]);
						pd.put("CYR", arr[1]);
						shipformationservice.updateShip(pd);
						}
					
					
				}
				return null;
		}
		
		//修改页面
				@RequestMapping(value="edit")
				public ModelAndView companytedit(){
				ModelAndView mv = this.getModelAndView();
				  try {
					  PageData pd = new PageData();
					  pd = this.getPageData();
					  pd= shipformationservice.seacById(pd);
					  List<PageData>list=shipformationservice.seachShip();
					  List<PageData>list1=shipformationservice.query();
					  mv.addObject("list", list);
					  mv.addObject("list1", list1);
					  mv.addObject("pd", pd);
					  mv.setViewName("/business/common/shipinformation/shipinformation_edit");
				} catch (Exception e) {
					e.printStackTrace();
				}
				  return mv;
				}
}
